package com.anjufang.service.system.impl;

import com.anjufang.entity.AdminMenu;
import com.anjufang.entity.AdminRoleMenu;
import com.anjufang.entity.AdminUserRole;
import com.anjufang.mapper.AdminMenuMapper;
import com.anjufang.mapper.AdminRoleMenuMapper;
import com.anjufang.mapper.AdminUserRoleMapper;
import com.anjufang.service.system.AdminMenuService;
import com.anjufang.utils.constant.Constant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class AdminMenuServiceImpl implements AdminMenuService {
	
	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;
	@Autowired
	private AdminRoleMenuMapper adminRoleMenuMapper;
	@Autowired
	private AdminMenuMapper adminMenuMapper;
	
	@Override
	public List<AdminMenu> getUserMenuList(Long userId) {
		
		//系统管理员，拥有最高权限
		if(userId == Constant.SUPER_ADMIN){
			return getAllMenuList(null);
		}
		Set<Long> menuList = new HashSet<>();
		List<AdminUserRole> roles = adminUserRoleMapper.selectList(new EntityWrapper<AdminUserRole>().where("user_id={0}", userId));
		for (AdminUserRole role : roles) {
			List<AdminRoleMenu> rMenus = adminRoleMenuMapper.selectList(new EntityWrapper<AdminRoleMenu>().where("role_id ={0}", role.getRoleId()));
			for (AdminRoleMenu menu : rMenus) {
				menuList.add(menu.getMenuId());
			}
		}
		return getAllMenuList(menuList);
	}
	
	/**
	 * 获取所有菜单列表
	 */
	private List<AdminMenu> getAllMenuList(Set<Long> menuIdList){
		
		//查询根菜单列表
		List<AdminMenu> menuList = queryListParentId(0L, menuIdList);
		//递归获取子菜单
		getMenuTreeList(menuList, menuIdList);
		return menuList;
	}
	
	/**
	 * 递归
	 */
	private List<AdminMenu> getMenuTreeList(List<AdminMenu> menuList, Set<Long> menuIdList){
		
		List<AdminMenu> subMenuList = new ArrayList<AdminMenu>();
		for(AdminMenu entity : menuList){
			if(entity.getType() == Constant.MenuType.CATALOG.getValue()){//目录
				entity.setList(getMenuTreeList(queryListParentId(entity.getId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
		}
		return subMenuList;
	}

	@Override
	public List<AdminMenu> queryListParentId(Long parentId, Set<Long> menuIdList) {
		
		List<AdminMenu> menuList = adminMenuMapper.selectList(new EntityWrapper<AdminMenu>().where("parent_id ={0}", parentId).orderBy("order_num"));
		if(menuIdList == null){
			return menuList;
		}
		
		List<AdminMenu> userMenuList = new ArrayList<>();
		for(AdminMenu menu : menuList){
			if(menuIdList.contains(menu.getId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	@Override
	@Transactional
	public Integer addAndUpdateRolePerm(AdminMenu menu) {
		
		//添加新的权限
		menu.insert();
		// logger.debug("添加新的权限【{}】",JSONObject.toJSONString(menu));
		//超级管理员绑定新的权限关系
		Map<String, Object> param = new HashMap<>();
		param.put("roleId", 1L);
		param.put("permissions", new Long[]{menu.getId()});
		Integer effect = adminRoleMenuMapper.setPermissions(param);
		return effect;
	}

	@Override
	public List<AdminMenu> menuList() {
		List<AdminMenu> menuList =adminMenuMapper.selectList(null);
		return menuList;
	}

	@Override
	public List<AdminMenu> selectList(Wrapper<AdminMenu> entityWrapper) {
		// 查询列表数据
		List<AdminMenu> menuList = adminMenuMapper.selectList(entityWrapper);

		// 添加顶级菜单
		AdminMenu root = new AdminMenu();
		root.setId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		return menuList;
	}

	@Override
	public void deleteBatch(Long[] ids) {
		
		adminMenuMapper.deleteBatch(ids);
	}

}

